<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      class="my_ipt"
      :value="inputValue"
      @change="inputChange"
    />
    <a-button type="primary" @click="addeletolist">添加事项</a-button>

    <a-list bordered :dataSource="showlist" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox
          :checked="item.done"
          @change="
            (e) => {
              checkboxchange(e, item.id);
            }
          "
          >{{ item.info }}</a-checkbox
        >
        <!-- 删除链接 -->
        <a slot="actions" @click="deleleinlist(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <span
          >总计:<span class="all">{{ list.length }}</span
          >项</span
        >
        <!-- 未完成的任务个数 -->
        <span>
          未完成:
          <span class="undone">
            {{ undonelength }}
          </span>
          项
        </span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            :type="viewkey === 'all' ? 'primary' : 'default'"
            @click="changelist('all')"
            >全部</a-button
          >
          <a-button
            :type="viewkey === 'undone' ? 'danger' : 'default'"
            @click="changelist('undone')"
            >未完成</a-button
          >
          <a-button
            :type="viewkey === 'done' ? 'primary' : 'default'"
            @click="changelist('done')"
            >已完成</a-button
          >
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex';

export default {
  name: 'app',
  data() {
    return {};
  },
  created() {
    this.$store.commit('initList');
  },
  computed: {
    ...mapState(['list', 'inputValue', 'viewkey']),
    ...mapGetters(['undonelength', 'showlist']),
  },
  methods: {
    inputChange(e) {
      this.$store.commit('setInputChange', e.target.value);
    },
    addeletolist() {
      if (this.inputValue.trim().length === 0) {
        return this.$message.warning('输入信息不能为空!');
      } else {
        this.$store.commit('addele');
      }
    },
    deleleinlist(e) {
      this.$confirm('请问确定要删除这个任务吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$message.success('删除成功！');
          this.$store.commit('delele', e);
          console.log(this);
        })
        .catch(() => {
          this.$message.info('已取消删除！');
        });
    },
    checkboxchange(e, id) {
      const fuxuan = {
        id: id,
        status: e.target.checked,
      };
      this.$confirm('请问您是否确定已完成该任务？', {
        confirmButtonText: '已完成',
        cancelButtonText: '未完成',
        type: 'warning',
      })
        .then(() => {
          this.$message.success(
            '恭喜您又完成了一个任务，离您的人生目标又近了一步！'
          );
          this.$store.commit('changebox', fuxuan);
          console.log(this);
        })
        .catch(() => {
          this.$message.info('加油！请继续努力');
        });
    },
    clear() {
      this.$confirm('此操作将永久清空所有已完成任务，请问您是否确定清空？', {
        confirmButtonText: '确定清空',
        cancelButtonText: '再考虑一下',
        type: 'warning',
      })
        .then(() => {
          this.$message.success('已为您清空所有已完成任务，请继续加油！！！');
          this.$store.commit('cleardone');
        })
        .catch(() => {
          this.$message.info('好的~~~');
        });
    },
    changelist(key) {
      this.$store.commit('listchange', key);
    },
  },
};
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.all {
  color: green;
  font-weight: bold;
}
.undone {
  color: red;
  font-weight: bold;
}
.done {
  background-color: green;
}
</style>
